package com.cmc.cloud.cmclink.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.DgProhibitedErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.dto.DgProhibitedItemUpdateDto;
import com.cmc.cloud.cmclink.doc.entity.DgProhibitedCountDO;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoDgDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.cmes.framework.common.exception.util.ServiceExceptionUtil;

import java.util.*;
import com.cmc.cloud.cmclink.doc.service.DgProhibitedItemService;
import com.cmc.cloud.cmclink.doc.entity.DgProhibitedItemDO;
import com.cmc.cloud.cmclink.doc.convert.DgProhibitedItemConvert;
import com.cmc.cloud.cmclink.doc.mapper.DgProhibitedItemMapper;

/**
 * 危险品禁接清单明细 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class DgProhibitedItemServiceImpl implements DgProhibitedItemService {

    @Resource
    private DgProhibitedItemMapper dgProhibitedItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAllDgProhibitedItem(List<DgProhibitedItemUpdateDto> dgProhibitedItemUpdateDtos, Long mainId) {
        // 更新
        List<DgProhibitedItemDO> updateObjs = DgProhibitedItemConvert.INSTANCE.convertUpdateList(dgProhibitedItemUpdateDtos);
        LambdaQueryWrapper<DgProhibitedItemDO> queryWrapper =
            Wrappers.<DgProhibitedItemDO>lambdaQuery().eq(DgProhibitedItemDO::getMainId, mainId);
        dgProhibitedItemMapper.delete(queryWrapper);
        updateObjs.forEach(obj -> obj.setId(null));
        dgProhibitedItemMapper.insertBatch(updateObjs);
    }

    @Override
    public void deleteDgProhibitedItem(Long id) {
        // 校验存在
        validateDgProhibitedItemExists(id);
        dgProhibitedItemMapper.deleteById(id);
    }

    private void validateDgProhibitedItemExists(Long id) {
        if (dgProhibitedItemMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(DgProhibitedErrorCodeConstants.DG_PROHIBITED_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public List<DgProhibitedItemDO> getDgProhibitedItemsByMainId(Long mainId) {
        LambdaQueryWrapper<DgProhibitedItemDO> queryWrapper = Wrappers.<DgProhibitedItemDO>lambdaQuery()
            .eq(DgProhibitedItemDO::getMainId, mainId).orderByAsc(DgProhibitedItemDO::getSubsidiaryRisk)
            .orderByAsc(DgProhibitedItemDO::getUnNo).orderByAsc(DgProhibitedItemDO::getFlashPoint);
        return dgProhibitedItemMapper.selectList(queryWrapper);
    }
    @Override
    public boolean existDgProhibitedItemsByMainId(Long mainId) {
        LambdaQueryWrapper<DgProhibitedItemDO> queryWrapper = Wrappers.<DgProhibitedItemDO>lambdaQuery()
                .eq(DgProhibitedItemDO::getMainId, mainId);
        return dgProhibitedItemMapper.exists(queryWrapper);
    }
    @Override
    public boolean existDgProhibitedItemsByMainIdAndDgDto(Long mainId, SpecialCargoDgDO dgDO) {
        LambdaQueryWrapper<DgProhibitedItemDO> queryWrapper = Wrappers.<DgProhibitedItemDO>lambdaQuery()
            .eq(DgProhibitedItemDO::getMainId, mainId)
                // 不限制UnNo null 或空 的 和匹配到的
                .and(wrapper ->
                        wrapper.isNull(DgProhibitedItemDO::getUnNo)
                                .or().eq(DgProhibitedItemDO::getUnNo, "")
                                .or().eq(DgProhibitedItemDO::getUnNo, dgDO.getUnNo()))
                .and(wrapper ->
                        wrapper.isNull(DgProhibitedItemDO::getFlashPoint)
                                .or().eq(DgProhibitedItemDO::getFlashPoint, dgDO.getFlashPoint()))
                .and(wrapper ->
                        wrapper.isNull(DgProhibitedItemDO::getSubsidiaryRisk)
                                .or().eq(DgProhibitedItemDO::getSubsidiaryRisk, "")
                                .or().eq(DgProhibitedItemDO::getSubsidiaryRisk, dgDO.getSubsidiaryRisk()))
                ;
        return dgProhibitedItemMapper.exists(queryWrapper);
    }

    @Override
    public List<DgProhibitedCountDO> getCountByMainIdList(List<Long> mainIds) {
        return dgProhibitedItemMapper.getCountByMainIdList(mainIds);
    }
}
